package cn.slahser.activity;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Hashtable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import cn.slahser.R;
import cn.slahser.serverEntity.Message;
import cn.slahser.utils.SlahserMetaData;

public class RegActivity extends Activity {

	protected EditText username = null;
	protected EditText password = null;
	protected EditText confirm = null;
	protected EditText address = null;
	protected EditText phone = null;
	protected RadioButton men = null;
	protected RadioButton women = null;
	protected Button submit = null;
	protected Button back = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_register);
		// 初始化界面
		this.initView();
		submit.setOnClickListener(new SubmitButtonOnClickListener());
		back.setOnClickListener(new BackButtonOnClickListener());
	}

	class SubmitButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// 表单层面的验证
			if (!password.getText().toString()
					.equals(confirm.getText().toString())) {
				Toast.makeText(RegActivity.this, "none same password!",
						Toast.LENGTH_SHORT).show();
				return;
			}
			// socket
			Socket socket = null;
			try {
				// 得到连接
				socket = new Socket(SlahserMetaData.IP,
						SlahserMetaData.SERVEPORT);
				ObjectOutputStream oout = new ObjectOutputStream(
						socket.getOutputStream());
				ObjectInputStream oin = new ObjectInputStream(
						socket.getInputStream());

				/***
				 * 请求信息
				 */
				Message message = new Message();
				// 构建数据
				Hashtable<String, Object> table = new Hashtable<String, Object>();
				table.put("uname", username.getText().toString());
				if (men.isChecked()) {
					table.put("sex", "男");
				} else {
					table.put("sex", "女");
				}
				table.put("phone", phone.getText().toString());
				table.put("address", address.getText().toString());
				table.put("password", password.getText().toString());
				// 填充请求数据
				message.setValue(table);
				// 设置请求类型
				message.setType(Message.REG);
				// 写出
				oout.writeObject(message);
				// 清空缓存
				oout.flush();

				/***
				 * 返回信息
				 */
				message = (Message) oin.readObject();
				// 返回信息携带ok
				if (message.getReturnValue().get("message").toString()
						.equalsIgnoreCase("ok")) {
					// 设置dialog上下文
					AlertDialog.Builder bb = new AlertDialog.Builder(
							RegActivity.this);
					bb.setTitle("Congratulations，register successfully！");
					bb.setMessage("your ID ："
							+ message.getReturnValue().get("userid"));
					final String userid = message.getReturnValue()
							.get("userid").toString();

					bb.setNeutralButton("Login now",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// 传递信息
									Intent in = new Intent();
									in.setClass(RegActivity.this,
											LoginActivity.class);
									in.putExtra("userid", userid);
									// 启动新activity
									startActivity(in);
									RegActivity.this.finish();
								}
							});

					bb.setNegativeButton("Exit",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									System.exit(0);
								}
							});
					bb.create().show();

				} else {
					Toast.makeText(RegActivity.this, "register failed!",
							Toast.LENGTH_SHORT).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(RegActivity.this, "no network connection!",
						Toast.LENGTH_SHORT).show();
			} finally {
				try {
					socket.close();
				} catch (IOException e) {
				}
			}
		}
	}

	class BackButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// 回到登录页面
			startActivity(new Intent(RegActivity.this, LoginActivity.class));
			finish();
		}
	}

	public void initView() {
		username = (EditText) findViewById(R.id.reg_edittext_uname);
		password = (EditText) findViewById(R.id.reg_edittext_pwd);
		confirm = (EditText) findViewById(R.id.reg_edittext_confirm);
		address = (EditText) findViewById(R.id.reg_edittext_add);
		phone = (EditText) findViewById(R.id.reg_edittext_phone);
		men = (RadioButton) findViewById(R.id.reg_radio_men);
		women = (RadioButton) findViewById(R.id.reg_radio_women);
		submit = (Button) findViewById(R.id.reg_button_submit);
		back = (Button) findViewById(R.id.reg_button_back);
	}

}
